# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2010  Jason Power <power.jg@gmail.com>
#    Copyright (C) 2010  Travis Gockel <travis@gockelhut.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from google.appengine.ext import db
import yaml
import dbmodel

def StringPropertyRepresenter(dumper, data):
	return dumper.represent_str(data)
yaml.add_representer(db.StringProperty, StringPropertyRepresenter)

def TextPropertyRepresenter(dumper, data):
	return dumper.represent_str(str(data))
yaml.add_representer(db.TextProperty, TextPropertyRepresenter)

def TextRepresenter(dumper, data):
	return dumper.represent_str(str(data))
yaml.add_representer(db.Text, TextRepresenter)

def BlobRepresenter(dumper, data):
	return dumper.represent_str(data)
yaml.add_representer(db.Blob, BlobRepresenter)

def DateTimePropertyRepresenter(dumper, data):
	return dumper.represent_str(str(data))
yaml.add_representer(db.DateTimeProperty, DateTimePropertyRepresenter)

def StringListPropertyRepresenter(dumper, data):
	data = [str(i) for i in data]
	return dumper.represent_list(data)
yaml.add_representer(db.StringListProperty, StringListPropertyRepresenter)

def FloatPropertyRepresenter(dumper, data):
	return dumper.represent_float(data)
yaml.add_representer(db.FloatProperty, FloatPropertyRepresenter)

def BooleanPropertyRepresenter(dumper, data):
	return dumper.represent_bool(data)
yaml.add_representer(db.BooleanProperty, BooleanPropertyRepresenter)

def ReferencePropertyRepresenter(dumper, data):
	return dumper.represent_str(data.key())
yaml.add_representer(db.ReferenceProperty, ReferencePropertyRepresenter)

def ListPropertyRepresenter(dumper, data):
	return dumper.represent_list(data)
yaml.add_representer(db.ListProperty, ListPropertyRepresenter)

def UserPropertyRepresenter(dumper, data):
	return dumper.represent_str(str(data.username))
yaml.add_representer(dbmodel.User, UserPropertyRepresenter)


def dumpAllGData(seq):
	for obj in seq:
		output += "\n---\n"
		output += obj.dumpYAML()

def loadData(data):
	return yaml.load(data)

def dumpObj(obj):
	return yaml.dump(obj)

